<?php
/**
 * Created by PhpStorm.
 * User: gm
 * Date: 2018-12-24
 * Time: 18:50
 */

namespace app\admin\model;

use think\Model;
use think\Db;

class ServiceType extends Model
{
    protected $table = 'cn_service_type';

    /**
     * 获取服务商的所有类型
     * @return array
     */
    public static function getAllType($lang = 'zh-cn')
    {

        if ($lang == 'zh-cn' || empty($lang)) {
            $data = Db::table('cn_service_types')
                ->field('service_type_id,service_type_name')
                ->where('is_del', 0)
                ->select();
            return $data;
            /*   return [
                   ['service_type_id' => '1', 'service_type_name' => '中国企业资信调查',],
                   ['service_type_id' => '2', 'service_type_name' => '海外企业资信调查',],
                   ['service_type_id' => '3', 'service_type_name' => '网站建设',],
                   ['service_type_id' => '4', 'service_type_name' => '网站数字推广',],
                   ['service_type_id' => '5', 'service_type_name' => '企业资质认证',],
                   ['service_type_id' => '6', 'service_type_name' => '实验室检测',],
                   ['service_type_id' => '7', 'service_type_name' => '物流',],
                   ['service_type_id' => '8', 'service_type_name' => '货代',],
                   ['service_type_id' => '9', 'service_type_name' => '海外展览',],
                   ['service_type_id' => '10', 'service_type_name' => '海外仓',],
                   ['service_type_id' => '11', 'service_type_name' => '外贸综合服务(关检汇税) ',],
                   ['service_type_id' => '12', 'service_type_name' => '其他',]
               ];*/
        } else {
            $data = Db::table('cn_service_types')
                ->field('service_type_id,service_type_name_en as service_type_name')
                ->where('is_del', 0)
                ->select();
            return $data;
            /*  return [
                  ['service_type_id' => '1', 'service_type_name' => 'Chinese company credit investigation',],
                  ['service_type_id' => '2', 'service_type_name' => 'Overseas company credit investigation',],
                  ['service_type_id' => '3', 'service_type_name' => 'Website building',],
                  ['service_type_id' => '4', 'service_type_name' => 'Website digital promotion',],
                  ['service_type_id' => '5', 'service_type_name' => 'Enterprise qualification certification',],
                  ['service_type_id' => '6', 'service_type_name' => 'Laboratory testing',],
                  ['service_type_id' => '7', 'service_type_name' => 'Logistics',],
                  ['service_type_id' => '8', 'service_type_name' => 'Freight forwarding',],
                  ['service_type_id' => '9', 'service_type_name' => 'Overseas exhibition',],
                  ['service_type_id' => '10', 'service_type_name' => 'Overseas warehouse',],
                  ['service_type_id' => '11', 'service_type_name' => 'Foreign trade comprehensive service: customs declaration、customs clearance、foreign exchange settlement、tax refund',],
                  ['service_type_id' => '12', 'service_type_name' => 'Other',]
              ];*/
        }

    }

    /**
     * 进行保存服务商的服务类型
     * @param $service_id
     * @param array $types
     * @return array|false
     */
    public static function saveServiceType($service_id, $types = [])
    {
        $old_type = self::getAllServiceType_($service_id);
        $del_types = array_diff($old_type, $types);//计算不存在的
        $news_types = array_diff($types, $old_type);//计算新的插入
        if (count($del_types)) {
            self::where('service_id', $service_id)
                ->whereIn('type', $del_types)
                ->update(['is_del' => 1]);
        }
        $newdata = [];
        if (count($news_types)) {
            foreach ($news_types as $key => $value) {
                $newdata[$key]['service_id'] = $service_id;
                $newdata[$key]['type'] = $value;
            }
            //$res = ServiceType::saveAll($newdata);
            //$this->saveAll($newdata);
            return Db::name('cn_service_type')->insertAll($newdata);

        }
        return true;
    }

    public static function getAllServiceType_($service_id)
    {

        $service_type = self::all(['service_id' => $service_id, 'is_del' => 0]);
        $service_type = $service_type ? json_decode(json_encode($service_type), true) : [];
        return $service_type ? array_column($service_type, 'type') : [];
    }


    /**
     * 计算服务商存在的服务类型
     * @param $service_id 服务商id
     * @return array
     */
    public static function getAllServiceType($service_id)
    {
        $service_type = DB::table('cn_service_type')->where(['service_id' => $service_id, 'is_del' => 0])->select();
        $service_type = $service_type ? json_decode(json_encode($service_type), true) : [];
        if ($service_type) {
            $service_type = array_column($service_type, 'type');
        } else {
            $service_type = [];
        }
        $new = [];
        foreach ($service_type as $key => $value) {
            $new[] = strval($value);
        };
        return $new;
    }


    /**
     * 获取服务商的类型id
     * @param $type_id
     * @return string
     */
    public static function getTypeName($type_id, $lang = 'zh-cn')
    {
        if (empty($type_id) || $type_id < 0 || $type_id > 12) {
            return '';
        }
        $type_name = '';
        $res = self::getAllType($lang);
        foreach ($res as $key => $value) {
            if ($value['service_type_id'] == $type_id) {
                $type_name = $value['service_type_name'];
                break;
            }
        }
        return $type_name;
    }


}